package tn.orange.canreminder.adapter;

import java.util.List;

import tn.orange.canreminder.R;
import tn.orange.canreminder.model.Place;
import tn.orange.canreminder.sqlite.PlaceDao;
import tn.orange.canreminder.sqlite.PlaceDaoImp;
import android.content.Context;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class PlaceAdapter extends BaseAdapter {

	private Context context;
	private List<Place> places;
	private LayoutInflater mInflater;
	private Location location;
	private List<tn.orange.canreminder.sqlite.Place> favPlaces;

	public PlaceAdapter(Context context, List<Place> places, Location location) {
		super();
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.places = places;
		this.location = location;

		PlaceDao dao = new PlaceDaoImp(context);
		favPlaces = dao.getAllPlace();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return places.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return places.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.place_item, null);
			holder = new ViewHolder();

			holder.distance = (TextView) convertView
					.findViewById(R.id.place_item_distance);
			holder.nom = (TextView) convertView
					.findViewById(R.id.place_item_nom);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.place_item_rating);
			holder.avis = (Button) convertView
					.findViewById(R.id.place_item_avis);

			holder.fav = (CheckBox) convertView
					.findViewById(R.id.place_ite_fav);

			holder.map = (ImageView) convertView
					.findViewById(R.id.place_item_map);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		final Place place = places.get(position);
		holder.fav.setTag((position));
		holder.avis.setTag(place.getId());
		holder.map.setTag(position);
		holder.fav.setChecked(false);
		holder.ratingBar.setEnabled(false);

		for (tn.orange.canreminder.sqlite.Place favPlace : favPlaces) {
			if (favPlace.getNom().equals(place.getName())) {
				holder.fav.setChecked(true);
				break;
			}
		}

		float rating = Float.parseFloat(place.getRating());

		holder.distance.setText("" + place.getDistance() + "m");
		holder.nom.setText(place.getName());
		holder.ratingBar.setRating(Float.parseFloat(place.getRating()));
		holder.ratingBar.setRating(rating);
		// holder.telephone.setText(place.getTelephone());
		return convertView;
	}

	static class ViewHolder {
		TextView nom;
		RatingBar ratingBar;
		TextView distance;
		TextView telephone;
		Button avis;
		ImageView map;
		CheckBox fav;

	}

}
